package edu.gatech.team404.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Random;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.gatech.team404.Account;
import edu.gatech.team404.Lab;
import edu.gatech.team404.Team;
import edu.gatech.team404.TeamAssignment;
import edu.gatech.team404.dal.CourseDAL;
import edu.gatech.team404.dal.LabDAL;
import edu.gatech.team404.dal.TeamDAL;
import edu.gatech.team404.tool.Log;
import edu.gatech.team404.tool.StatusCode;

/**
 * Servlet implementation class Authenticate
 * @author Michael Saul
 * @version 1.0
 */
@WebServlet("/RandomAssign")
public class RandomAssign extends HttpServlet {

	/** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
	protected void processRequest(HttpServletRequest request, 
								  HttpServletResponse response)
								  throws IOException, NumberFormatException {
        response.setContentType("text/html;charset=UTF-8");
        final PrintWriter out = response.getWriter();
        final HttpSession session = request.getSession(true);
        final TeamDAL teamDAL = new TeamDAL();
        final LabDAL labDAL = new LabDAL();
        final CourseDAL courseDAL = new CourseDAL();
        final String courseName = request.getParameter("courseName");
        try {
        	final String labID = request.getParameter("labID");
        	
        	final ArrayList<Account> students = (ArrayList<Account>) courseDAL.getStudents(courseName);
        	double numTeams = (double)students.size() / 5.0;
        	int realNumTeams = 0;
        	if(numTeams%1.0 != 0.0){
        		numTeams = numTeams + 1;
        	}
        	while(numTeams > 0){
        		realNumTeams++;
        		numTeams = numTeams - 1;
        	}
        	
        	boolean randomCheck = true;
        	boolean superCheck = true;
        	Random rand = new Random();
        	ArrayList<Account> assignedStuds = new ArrayList<Account>();
        	Account stud = students.get(rand.nextInt(students.size()));
        	for(int i = 0; i < realNumTeams; i++){
        		Team team = new Team(labID, null, "Team " + i);
        		
        		for(int j = 0; j < 5; i++){
        			if(assignedStuds.size() == students.size()){
        				break;
        			}
        			
        			while(assignedStuds.contains(stud)){
        				 stud = students.get(rand.nextInt(students.size()));
        			}
        			
        			randomCheck = teamDAL.addMember(stud.getUsername(), team);
        			if(randomCheck == true){
        				assignedStuds.add(stud);
        			}
        			
        		}
        		
        		if(randomCheck == false)
        			superCheck = false;
        	}
  
        	if(superCheck){
        		session.setAttribute("randomStatus", true);
	        }
	        else{
	        	session.setAttribute("randomStatus", false);
	        }
        } finally {
        	session.setAttribute("feed", StatusCode.UPDATE.toString());
        	response.sendRedirect("team.jsp?course=" + courseName);
            out.close();
        }
    }

	/** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, NumberFormatException {
        processRequest(request, response);
    } 

	/** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, NumberFormatException {
        processRequest(request, response);
    }
    
}